<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-03-17
 * Time: 19:22
 */

namespace app\api\home;


use app\api\model\CmsBanner;
use app\cms\model\CmsArticle;
use \think\facade\Cache;
use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 获取Banner接口
    */
    public function index($number = 5)
    {
        $data = CmsBanner::where(['status'=>1])
            ->limit($number)
            ->select();
        return json(["code" => 1,'data'=>$data]);
    }

    /**
     * 获取文章接口
     */
    public function article()
    {
        $data = CmsArticle::where(['status' => 1])->select();
        return json(["code" => 1, 'data' => $data]);
    }

    /**
     * 获取浏览数接口
     */
    public function reading(Request $request)
    {
        $id = $request->post();
        $contentData = Db::table('hisi_cms_article')->where(['id'=>$id])->find();
        $result = Db::table('hisi_cms_article')->where(['id'=>$contentData['id']])
            ->update(['reading'=>$contentData['reading']+1]);
        if ($result){
            return json(["code" => 1,'msg'=>'浏览数+1']);
        }
    }

    /**
     * 获取收藏数接口
     */
    public function collection(Request $request)
    {
        $data = $request->post();
        $contentData = Db::table('hisi_cms_article')->where(['id'=>$data['id']])->find();
        if($data['collected'] == 'true'){
            $result = Db::table('hisi_cms_article')->where(['id'=>$contentData['id']])
                ->update(['collection'=>$contentData['collection']+1]);
            $openId = Cache::get('openID');
            $data = [
                "openId" => $openId,
                "contentId" => $data['id']
            ];
            Db::table('hisi_cms_collector')->insert($data);
            if ($result){
                return json(['code' => 1,'msg'=>'成功']);
            }
        }
        if ($data['collected'] == 'false'){
            $result = Db::table('hisi_cms_article')->where(['id'=>$contentData['id']])
                ->update(['collection'=>$contentData['collection']-1]);
            if($result){
                return json(['code' => 1,'msg'=>'成功']);
            }
        }
    }


}